// pages/paperAnswer/paperAnswer.js
var wxCharts = require('../../../resource/wxcharts.js'); 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paper:{},
    totalQuestion: [],
    answerList: [],
    currentTab: true,
    accuracy: 0,
    errorRate: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading()

      let paper = wx.getStorageSync("paper")
      let totalQuestion = wx.getStorageSync("totalQuestion")
      let answerList = wx.getStorageSync("answerList")

      this.setData({ paper: paper, totalQuestion: totalQuestion, answerList: answerList })

      wx.removeStorageSync('paper')
      wx.removeStorageSync('totalQuestion')
      wx.removeStorageSync('answerList')

      wx.hideLoading()

      console.log(this.data.paper)
      console.log(this.data.totalQuestion)
      console.log(this.data.answerList)

      this.result()
      this.switchTabStatus()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  backAnswer: function(){
    wx.switchTab({
      url: '/pages/paper/paperTests/paperTests',
    })
  },

  switchTabStatus: function(){
    let currentTab = !this.data.currentTab
    let that = this
    this.setData({ currentTab: currentTab })
    if(this.data.currentTab){
      wx.showLoading()

      let paper = wx.getStorageSync("paper")
      let totalQuestion = wx.getStorageSync("totalQuestion")
      let answerList = wx.getStorageSync("answerList")

      this.setData({ paper: paper, totalQuestion: totalQuestion, answerList: answerList })

      wx.removeStorageSync('paper')
      wx.removeStorageSync('totalQuestion')
      wx.removeStorageSync('answerList')

      wx.hideLoading()

      console.log(this.data.paper)
      console.log(this.data.totalQuestion)
      console.log(this.data.answerList)
    }else{
      new wxCharts({

        canvasId: 'pieCanvas',
    
        type: 'ring',
    
        series: [{
            name: '正确题目',
            data: that.data.accuracy,
            color: "#FFA500"
        }, {
    
            name: '错误题目',
            data: that.data.errorRate,
            color: '#00FF00'
        }],
    
        width: 320,
    
        height: 200,
    
        dataLabel: false
    
      });
    }
  },

  result:function(){
    /**
     * 计算正确率
     */
    let answerList = this.data.answerList
    let questionNum = this.data.answerList.length; // 题目总数
    let rightNum = 0;
    let badNum = 0;
    for(let i = 0; i < answerList.length; i ++){
      let answer = this.getAnswer(answerList[i].id, "single")
      console.log(answer)
      if(answerList[i].type == "single"){
        
        if(answer.single_choice_nswer == answerList[i].answer){
          rightNum += 1
        }else{
          badNum -= 1
        }

      }else if(answerList[i].type == "many"){

        if(answer.many_choice_answer == answerList[i].answer){
          rightNum += 1
        }else{
          badNum -= 1
        }

      }else if(answerList[i].type == "write"){

        rightNum += 1

      }

    }

    this.setData({ accuracy: (rightNum / questionNum).toFixed(2) * 100 })
    this.setData({ errorRate: (1 - (rightNum / questionNum).toFixed(2)) * 100 })

  },

  getAnswer:function(id, type){
    for(let i = 0; i < this.data.totalQuestion.length; i ++){
      if(this.data.totalQuestion[i].id == id && this.data.totalQuestion[i].type == type){
        return this.data.totalQuestion[i]
      }
    }
  }
  
})